//归整表格标签
function form_data(data){
		var reg = new RegExp("<td2>","gm");//g,表示全部替换。
				data=data.replace(reg,"<td>");
				var reg = new RegExp("<tr1>","gm");//g,表示全部替换。
				data=data.replace(reg,"<tr>");
				var reg = new RegExp("</td3>","gm");//g,表示全部替换。
				data=data.replace(reg,"</td>");
				var reg = new RegExp("</tr4>","gm");//g,表示全部替换。
				data=data.replace(reg,"<tr>");
				return data;
}
//根据userAgent判断是否是手机 
function ismobile(){
 var userAgentInfo = navigator.userAgent; 
 var mobileAgents = [ "Android", "iPhone", "Windows Phone", "iPad","iPod"]; 
 var mobile_flag = false;
 for (var v = 0; v < mobileAgents.length; v++) { 
 if (userAgentInfo.indexOf(mobileAgents[v]) > 0) { 
 mobile_flag = true; 
 break; 
 } 
 }
return mobile_flag; 
}
//获取cookie
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
{return unescape(arr[2]);
}else{
return null;}
}
//检查CODE是否被激活
function check_qrcode_connect(param,interval_id,timeout_id){
$.ajax({
	type: 'GET',
	async: false, 
	url: 'index.php?param='+param,
	data: { auth_code:true },
	beforeSend:function(){
	console.log("验证请求中...");
	},
	success:function(data){
	if(data=="1"){
	window.clearInterval(interval_id);
	if(!ismobile()){window.clearTimeout(timeout_id);}
	if(!ismobile()){
	remove_qrcode(2,param);//PC端的移除操作
	}else{
	remove_qrcode(3,param);//手机端的移除操作
	}
	}
	}
		});
	
}
//获取可用空间
function get_file_space(param){
$.ajax({
	type: 'GET',
	async: false, 
	url: 'index.php',
	data: { file_space:true,param:param },
	beforeSend:function(){
	console.log("数据提交中...");
	},
	success:function(data){
	$("div#dir_space").html(data);
			}
		});
}
function get_file_list(param){
$.ajax({
	type: 'POST',
	async: false, 
	url: 'index.php?param='+param,
	data: { show_file_list:true },
	beforeSend:function(){
	console.log("数据提交中...");
	},
	success:function(data){
	var file_list=$("div#file_list",data).html();
	file_list=form_data(data);
	$("tbody#file_list").html(file_list);
			}
		});
}
//生成二维码
function makeCode (param) {
	if(!param){
	var url="http://localhost/qrcode/index.php?code_active&param=";
	var code_text = Math.random().toString(36).substring(2);
	var newDay = new Date(); 
	code_text = code_text + Number(newDay);
	console.log(code_text);
	qrcode.makeCode(url+code_text);
	$.ajax({
	type: 'GET',
	async: false, 
	url: 'index.php',
	data: { insert_code:true,param:code_text },
	beforeSend:function(){
	console.log("数据提交中...");
	},
	success:function(data){
	console.log("Data:"+data);
			}
		});
var timeout_id=setTimeout(function(){remove_qrcode(1,code_text);window.clearInterval(interval_id);},60000*5);//二维码生命周期(5min)
//电脑端的检测
var interval_id=setInterval(function(){check_qrcode_connect(code_text,interval_id,timeout_id);},5000);//检测二维码是否被激活
}else{
	var url="http://localhost/qrcode/index.php?setcookie&param=";
	qrcode.makeCode(url+param);
}
}
//移除二维码
//模式一:二维码失效
//模式二:启用文件列表[电脑]
//模式三:启用文件列表[手机]
function remove_qrcode(mode,param){
if(!arguments[0]) mode = 1;
qrcode.clear();
if(mode==1){
var template="<div class='icon-refresh'></div>二维码已失效,请刷新后重试";
$("div#qrcode").attr("style","width:100%;height:100px; position:relative;top:100px;margin:auto;text-align:center;");
$("div#qrcode").html(template);
$.ajax({
	type: 'POST',
	async: false, 
	url: 'index.php?param='+param,
	data: { del_code:true },
	beforeSend:function(){
	console.log("数据提交中...");
	},
	success:function(data){
	console.log("Data:"+data);
			}
		});
}
if(mode==2){
console.log("文件系统已启用");
$("div#show_starter").attr("style","display:none;");
$("div#show_file").attr("style","");
$("#upload_button").attr("onclick","upload_file('"+param+"');");
var interval_id=setInterval(function(){
get_file_space(param);
get_file_list(param);
},5000);	
}
if(mode==3){
$("div#show_starter").attr("style","display:none;");
$("div#show_file").attr("style","");
$("#upload_button").attr("onclick","upload_file('"+param+"');");
var interval_id=setInterval(function(){
get_file_space(param);
get_file_list(param);
},5000);
}
}

function del_file(param,filename){
$.ajax({
	type: 'POST',
	async: false, 
	url: 'index.php?param='+param,
	data: { del_file:filename },
	beforeSend:function(){
	console.log("删除请求提交中...");
	},
	success:function(data){
console.log(data);
			}
		});
get_file_space(param);
get_file_list(param);
}

function upload_file(param){
	$("#upload_button").attr("class","suc_button disabled");
	var formData = new FormData($('form')[0]);
 formData.append('file',$(':file')[0].files[0]);
 $.ajax({
  url:'index.php?upload_file&param='+param,
  type: 'POST',
  data: formData,
  contentType: false,
  processData: false,
  success:function(data){
	  console.log(data);
	  $("#upload_button").attr("class","suc_button");
  }
 });
get_file_space(param);
get_file_list(param);
}